﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "UiAdapter.h"
#include "IDocument.h"
#include "ElementId.h"

#include "GDFoundation/ParameterValueUtils.h"
#include "IDocument.h"
#include "UniIdentity.h"
#include "IParameter.h"

namespace Sample
{
    class UiAdapter::Impl
    {
    public:
        static Impl& Get() {
            static Impl object{};
            return object;
        }

        gcmp::OwnerPtr<gdmp::IParameterDisplayListRegister> m_oparameterDisplayListRegister{};
        std::function<void(gcmp::ActionModifyInput& input)> m_addEventHandlerFunc;
    };

    class UiAdapter::Modifyer
    {
    public:
        static Modifyer& Get() {
            static Modifyer object{};
            return object;
        }

        bool AppendContextMenu(gcmp::IDocument* pDocument, gcmp::ElementId elementId, gcmp::IMenuItemContainerDefinition* pMenuItemContainer) const
        {
            DBG_WARN_AND_RETURN_FALSE_UNLESS(pDocument, L"断言`pDocument`失败",L"GDMPLab",L"2024-03-30");
            DBG_WARN_AND_RETURN_FALSE_UNLESS(pMenuItemContainer, L"断言`pMenuItemContainer`失败",L"GDMPLab",L"2024-03-30");
            gcmp::IElement* pElement = pDocument->GetElement(elementId);
            if (!pElement) return false;
            for (auto& obj : m_objects) {
                if (!obj) continue;
                if (obj->AppendContextMenu(pElement, pMenuItemContainer)) {
                    return true;
                }
            }
            return false;
        }
        std::vector<gcmp::OwnerPtr<IElementContextMenuStrategy>> m_objects;
    };

    gdmp::IParameterDisplayListRegister* UiAdapter::GetDisplayListRegister()
    {
        return Get().m_oparameterDisplayListRegister.get();
    }

    void UiAdapter::SetDisplayListRegister(gcmp::OwnerPtr<gdmp::IParameterDisplayListRegister> opObject)
    {
        Get().m_oparameterDisplayListRegister = std::move(opObject);
    }

    void UiAdapter::RegisterContextMenuStrategy(gcmp::OwnerPtr<Sample::IElementContextMenuStrategy> opObject)
    {
        GetModifyer().m_objects.emplace_back(TransferOwnership(opObject));
    }

    void UiAdapter::ApplyContextMenuStrategy(gcmp::IDocument* pDocument, gcmp::ElementId elementId, gcmp::IMenuItemContainerDefinition* pMenuItemContainer)
    {
        GetModifyer().AppendContextMenu(pDocument, elementId, pMenuItemContainer);
    }

    void UiAdapter::SetActionModifyAddEventHandlerCallBack(std::function<void(gcmp::ActionModifyInput& input)> func)
    {
        Get().m_addEventHandlerFunc = func;
    }

    void UiAdapter::ApplyPickPostProcessEventHandler(gcmp::ActionModifyInput& input)
    {
        if (Get().m_addEventHandlerFunc)
        {
            Get().m_addEventHandlerFunc(input);
        }
    }

    UiAdapter::Impl& UiAdapter::Get()
    {
        return Impl::Get();
    }

    UiAdapter::Modifyer& UiAdapter::GetModifyer()
    {
        return Modifyer::Get();
    }


    bool Sample::UiAdapter::GetPresetValuesGroupId(const gcmp::IDocument* pDocument, const gcmp::IElement* pElement, Int32& groupId)
    {

        return gdmp::ParameterValueUtils::GetPresetValuesGroupId(pDocument, pElement, groupId);
    }

    bool Sample::UiAdapter::SetPresetParameter(const gcmp::IDocument* pDocument, const gcmp::UniIdentity& categoryUid, Int32 presetValuesGroupId, const gcmp::IParameter* pParameter)
    {
        return gdmp::ParameterValueUtils::SetPresetParameter(pDocument, categoryUid, presetValuesGroupId, pParameter);
    }
}
